Skip to content

Compute moments on grids with points of shape (N,) - #351

Open
syahra712 wants to merge 2 commits into
theochem:masterfrom
syahra712:fix-moments-1d-points
Open

syahra712 wants to merge 2 commits into
theochem:masterfrom
syahra712:fix-moments-1d-points

Conversation

@syahra712

Copy link
Copy Markdown
Contributor

Summary

Grid.moments raises IndexError on a grid whose points have shape (N,). That includes every OneDGrid (GaussLegendre, Trapezoidal, ...), even though the constructor accepts 1D point arrays:

import numpy as np
from grid.onedgrid import GaussLegendre

g = GaussLegendre(5)          # g.points.shape == (5,)
g.moments(2, np.array([[0.0]]), np.ones(5), type_mom="cartesian")
# IndexError: tuple index out of range   (self.points.shape[1])

The same points as shape (N, 1) work since #335 and give [2, 0, 2/3].

  • Grid.moments: uses self.points.reshape(len(self.points), -1) locally, so (N,) points are treated as N points in one dimension, like (N, 1). The dimension check, the centering and the order generation all use it. Grids with 2D points are unchanged.

Tests

TestGrid1D.test_cartesian_moments_flat_points checks that a Grid and a OneDGrid with (N,) points give the same moments as the (N, 1) grid. It fails on master with the IndexError above and passes with this change. test_grid.py and test_atomgrid.py: 94 passed. ruff and black are clean on the changed lines.

Grid.moments read self.points.shape[1], so a grid whose points have
shape (N,), such as every OneDGrid, raised IndexError. Treat those
points as N points in one dimension, like shape (N, 1).
@PaulWAyers
PaulWAyers requested review from marco-2023 and a balanced review from Copilot November 18, 2026 21:19
@PaulWAyers

Copy link
Copy Markdown
Member

This seems straightforward and OK. I would probably have done the simpler option of merely returning an error if the moments weren't 3D, but this is OK. What do you think @marco-2023

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The unconditional reshape in src/grid/basegrid.py breaks Cartesian moments for supported empty 2D grids, which previously returned zeros.

1 open issue
What changed in this PR

Extends Grid.moments to accept flat (N,) point arrays used by OneDGrid, building on the earlier fix for one-dimensional Cartesian orders.

Changes:

  • Normalizes points locally for dimension checks, order generation, and centering.
  • Adds regression coverage comparing flat Grid and OneDGrid moments with (N, 1) grid moments.
File Description
src/​grid/​tests/​test_grid.py Tests Cartesian moment equivalence for flat and column-shaped points.
src/​grid/​basegrid.py Normalizes point arrays before computing moments.

Review effort: Balanced

Comment thread src/grid/basegrid.py Outdated

"""
# Points of shape (N,), as in OneDGrid, are N points in one dimension
points = self.points.reshape(len(self.points), -1)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in 96d54eb: 2D points are used as they are, and only points of shape (N,) get the extra axis. Added test_moments_empty_localgrid for every grid dimension; it fails with the previous commit ("cannot reshape array of size 0") and passes now.

Reshaping every points array with reshape(len(points), -1) fails for an
empty local grid of shape (0, 3), whose moments used to be zero. Only
add the singleton axis for points of shape (N,).
@PaulWAyers
PaulWAyers requested a review from FarnazH November 20, 2026 21:03

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants